$(document).ready(function(){

	if ($('#multilink').length) {
		var sel_servers = $('#sel-servs').clone();
		$('#sel-servs').remove();	
	}
	

	console.log(sel_servers);

	$('#bmultilink').click(function(){

		var links = $('#multilink').val();

		if (links !== '') {
			var array = links.split('\n');
			var html_links = '';

			var clone;
			var html_option;
			for(var i in array){
				clone = sel_servers.clone();
				html_option = '';

				for(var opt in clone[0]){
					if (opt > 0) {
						if (array[i].toLowerCase().indexOf(clone[0][opt].text.toLowerCase()) > -1)
							html_option += '<option value="'+clone[0][opt].value+'" selected>'+clone[0][opt].text+'</option>';
						else
							html_option += '<option value="'+clone[0][opt].value+'">'+clone[0][opt].text+'</option>';
					}
				}

				html_links += '<select name="sel-servers[]" class="select float-r" id="sel-servs">' + html_option + '</select>';
				html_links += '<label for="url_torrent">URL|Link Archivo</label>';
				html_links += '<input type="text" class="size80" name="url_torrent[]" value="'+array[i]+'" maxlength="150"/>';
				html_links += '<label for="url_subtitle">URL|link Subtitulos</label>';
				html_links += '<input type="text" class="size80" name="url_subtitle[]" value="" maxlength="200"/>';
			}
			$('#links-fields').html('');
			$(html_links).appendTo('#links-fields');
		} else {
			$('#links-fields').html('');
		}

	});
});